local scene = gamestate.new()

local layout = {
    localGame = {text = "本地游戏"},
    createRoom = {text = "创建房间"},
    joinRoom = {text = "加入房间"},
    mapEditor = {text = "地图编辑"},
    setting = {text = "设定"},
    close = {},
    userID = {text = "访客"},
    register = {text = "注册"},
    login = {text = "登录"} 
    }

local x = 200
local y = 200
local w = 200
local h = 50
local off = 20
local sw,sh

function layout:update()
    local f = love.graphics.getFont()
    love.graphics.setFont(texture.font)
    local offy = 0
    suit.Label("City Battle Teamwork",x-100,y-100,w+200,h)
    local b = self.localGame
    local r = suit.Button(b.text,b,x,y+offy,w,h)
    if r.entered then
        sound:play("hover")
    end
    if r.hit then
        gamestate.switch(gameState.local_gm_select,scene.game)
    end
    offy = offy + (h + off)
    local b = self.createRoom
    local r = suit.Button(b.text,b,x,y+offy,w,h)
    if r.entered then
        sound:play("hover")
    end
    if r.hit and user.id then
        gamestate.switch(gameState.gm_select,scene.game)
    end

    offy = offy + (h + off)
    local b = self.joinRoom
    local r = suit.Button(b.text,b,x,y+offy,w,h)
    if r.entered then
        sound:play("hover")
    end
    if r.hit and user.id then
        gamestate.switch(gameState.room_select,scene.game)
    end

    offy = offy + (h + off)
    local b = self.mapEditor
    local r = suit.Button(b.text,b,x,y+offy,w,h)
    if r.entered then
        sound:play("hover")
    end
    if r.hit then
        gamestate.switch(gameState.mapeditor)
    end

    offy = offy + (h + off)
    local b = self.setting
    local r = suit.Button(b.text,b,x,y+offy,w,h)
    if r.entered then
        sound:play("hover")
    end
    if r.hit then
        gamestate.switch(gameState.setting,scene.game)
    end

    

    if suit.Button("X",self.close,sw*GlobalScale - 60,10,50,50).hit then
        love.event.quit()
    end
    
    local name = user.id or self.userID.text
    local text = name..",您好！"
    local tw = love.graphics.getFont():getWidth(text) + 20
    suit.Label(text,10,10,tw,50)

    if not user.id then
       
        local r = suit.Button(self.register.text,self.register,tw+20,10,100,50)
        if r.entered then
            sound:play("hover")
        end
        if r.hit then
            gamestate.switch(gameState.register,scene.game)
        end
        local r = suit.Button(self.login.text,self.login,tw+130,10,100,50)
        if r.entered then
            sound:play("hover")
        end
        if r.hit then
            gamestate.switch(gameState.login,scene.game)
        end
    end
    love.graphics.setFont(f)
end

function scene:enter(from,game)
    collectgarbage("collect")
    sw,sh = love.graphics.getDimensions()
    x = sw*GlobalScale/2 - w/2
    self.game = game or game_mode.background()
end

function scene:update(dt)
    layout:update(dt)
    self.game:update(dt)
end

function scene:draw()
    self.game:draw()
    suit.draw()
end

return scene